package com.tracbds.core.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.tracbds.core.bean.VehicleLocation;

/**
 * Redis服务类，用于推送消息到Redis队列
 * @author lingx.com
 */
@Component
public class RedisService {
    
    private static final Logger logger = LoggerFactory.getLogger(RedisService.class);
    
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    
    /**
     * 推送车辆定位数据到Redis队列
     * @param vehicleLocation 车辆定位数据
     */
    public void pushVehicleLocationData(VehicleLocation vehicleLocation) {
        try {
            String jsonData = JSON.toJSONString(vehicleLocation);
            redisTemplate.opsForList().rightPush("vehicle_location_data", jsonData);
            logger.debug("推送车辆定位数据到Redis队列成功: {}", jsonData);
        } catch (Exception e) {
            logger.error("推送车辆定位数据到Redis队列失败: {}", e.getMessage(), e);
        }
    }
    
    /**
     * 推送车辆定位数据到Redis队列（使用Map数据）
     * @param locationData 定位数据Map
     */
    public void pushVehicleLocationDataFromMap(java.util.Map<String, Object> locationData) {
        try {
            // 从Map数据构建VehicleLocation对象
            VehicleLocation vehicleLocation = buildVehicleLocationFromMap(locationData);
            if (vehicleLocation != null) {
                pushVehicleLocationData(vehicleLocation);
            }
        } catch (Exception e) {
            logger.error("从Map数据推送车辆定位数据到Redis队列失败: {}", e.getMessage(), e);
        }
    }
    
    /**
     * 从Map数据构建VehicleLocation对象
     * @param locationData 定位数据Map
     * @return VehicleLocation对象
     */
    private VehicleLocation buildVehicleLocationFromMap(java.util.Map<String, Object> locationData) {
        try {
            String vehicleId = (String) locationData.get("tid");
            if (vehicleId == null) {
                logger.warn("车辆ID为空，跳过推送");
                return null;
            }
            
            Double latitude = (Double) locationData.get("lat");
            Double longitude = (Double) locationData.get("lng");
            String gpstime = (String) locationData.get("gpstime");
            Float speedFloat = (Float) locationData.get("speed");
            Integer direction = (Integer) locationData.get("direction");
            
            // 转换速度从Float到Integer
            Integer speed = speedFloat != null ? speedFloat.intValue() : 0;
            
            // 解析GPS时间
            java.util.Date uploadTime = null;
            if (gpstime != null && gpstime.length() >= 14) {
                try {
                    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
                    uploadTime = sdf.parse(gpstime);
                } catch (Exception e) {
                    logger.warn("解析GPS时间失败: {}", gpstime);
                    uploadTime = new java.util.Date();
                }
            } else {
                uploadTime = new java.util.Date();
            }
            
            return new VehicleLocation(vehicleId, latitude, longitude, uploadTime, speed, direction);
        } catch (Exception e) {
            logger.error("构建VehicleLocation对象失败: {}", e.getMessage(), e);
            return null;
        }
    }
} 